package com.dzm.Test05.ui;

import com.dzm.Test05.dao.DeptDao;
import com.dzm.Test05.dao.EmpDao;
import com.dzm.Test05.dao.UserDao;
import com.dzm.Test05.dao.WorkDao;
import com.dzm.Test05.dao.impl.DeptDaoImpl;
import com.dzm.Test05.dao.impl.EmpDaoImpl;
import com.dzm.Test05.dao.impl.UserDaoImpl;
import com.dzm.Test05.dao.impl.WorkDaoImpl;
import com.dzm.Test05.entity.Dept;
import com.dzm.Test05.entity.Emp;
import com.dzm.Test05.entity.EmpVo;
import com.dzm.Test05.entity.User;

import java.util.List;
import java.util.Scanner;

/**
 * 作者：Dzm
 * 时间：2024-07-26-20:02
 * 功能：
 */

public class UserUi {
    private UserDao userDao;
    private EmpDao empDao;
    private DeptDao deptDao;
    private WorkDao workDao;
    private Scanner scanner = new Scanner(System.in);
    private boolean login = true; //设置了默认登录

    {
        userDao = new UserDaoImpl();
        empDao = new EmpDaoImpl();
        deptDao = new DeptDaoImpl();
        workDao = new WorkDaoImpl();
    }
    private void check() {
        if(!login) {
            System.out.println("未登录！");
        }
    }

    public void show() {
        System.out.println("///////////////////////// JDBC数据库查询信息系统 /////////////////////////");
        System.out.println("\t1.用户登录");
        System.out.println("\t2.员工列表");
        System.out.println("\t3.部门列表");
        System.out.println("\t4.查看员工及其部门");
        System.out.println("\t5.查询部门下员工");
        System.out.println("\t6.工资范围查询");
        System.out.println("/////////////////////////////////////////////////////////////////////////");


        userSelect();
    }

    private void userSelect() {
        System.out.print("请输入选项：");
        int choice = scanner.nextInt();
        switch (choice) {
            case 1:
                if(login) {
                    System.out.println("已登录！");
                } else{
                    login();
                }
                break;
            case 2:
                check();
                if(login)showAllEmp();
                break;
            case 3:
                check();
                if(login)showAllDept();
                break;
            case 4:
                check();
                if(login)showEmpInfo();
                break;
            case 5:
                check();
                if(login)showDeptEmps();
                break;
            case 6:
                check();
                if(login)showEmpsBetween();
                break;
            default:
                System.out.println("请正确输入！");
        }

        System.out.println("是否继续?（y/n）");
        String answer = scanner.next();
        if(answer.equalsIgnoreCase("y")) {
            userSelect();
        }
    }

    private void showEmpsBetween() {
        System.out.print("请输入最低工资：");
        String min = scanner.next();
        System.out.print("请输入最高工资：");
        String max = scanner.next();
        List<Emp> empList = empDao.getEmpsBetween(min, max);
        empList.forEach(System.out::println);
    }

    private void showDeptEmps() {
        System.out.print("请输入部门编号：");
        String id = scanner.next();
        List<Emp> empList = deptDao.getAllEmpByDept(id);
        empList.forEach(System.out::println);
        empList.sort((a, b) -> {return (int) (a.getSalary()-b.getSalary());});
        Double sum = new Double(0);
        for (Emp emp : empList) {
            sum += emp.getSalary();
        }

        System.out.println("最低工资为：" + empList.get(0).getSalary() + ", 最高工资为：" + empList.get(empList.size()-1).getSalary() + ", 平均工资为：" + sum/empList.size());
    }

    private void showEmpInfo() {
        System.out.print("请输入员工编号：");
        String eid = scanner.next();
        EmpVo empVo = empDao.getEmpInfoById(eid);
        System.out.println(empVo);

    }

    private void showAllDept() {
        List<Dept> deptList = deptDao.getAllDepts();
        deptList.forEach(System.out::println);
    }

    private void showAllEmp() {
        List<Emp> empList = empDao.getAllEmps();
        empList.forEach(System.out::println);
    }

    private void login() {
        System.out.print("请输入用户名：");
        String username = scanner.next();
        System.out.print("请输入密码：");
        String password = scanner.next();
        User user = new User(null, null, username, password);
        boolean l = userDao.selectUser(user);

        if(l) { //登录成功则进入主页
            System.out.println("登录成功！");
            login = true;
            show();
            return;
        }
        //登录失败重新登录
        System.out.println("登录失败！");
        login();
    }
}
